/*  Init
 *
 *  This routine is the initialization task for this test program.
 *  It is called from init_exec and has the responsibility for creating
 *  and starting the tasks that make up the test.  If the time of day
 *  clock is required for the test, it should also be set to a known
 *  value by this function.
 *
 *  Input parameters:  NONE
 *
 *  Output parameters:  NONE
 *
 *  COPYRIGHT (c) 1989-1999.
 *  On-Line Applications Research Corporation (OAR).
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.com/license/LICENSE.
 *
 *  $Id: init.c,v 0.01 2009/08/25 roxana $
 */

#define CONFIGURE_INIT
#include "system.h"
#include <stdio.h>
#include <stdlib.h>
#if BSP_HAS_FRAME_BUFFER
#include <rtems/fb.h>
#include <fcntl.h>
#endif

/*draw a square on a specific location*/
void *draw_square(int fd, int x, int y, int size){
	int i = 0, location=0;
	char * buff = 0x00; 
	lseek(fd,0, SEEK_SET);
	
	for (i = 0; i< size; i++){
        	//set cursor at the begining of the framebuffer
        	location = x + (y+i) * 80;
		//set cursor at the x, y coord
		lseek(fd, location, SEEK_SET);
		write(fd, &buff, 1);
	}
}

rtems_task Init( rtems_task_argument ignored )
{

#if !BSP_HAS_FRAME_BUFFER
	printf("THERE IS NO FRAME BUFFER SUPPORT\n");
#else
	static int fb =0;		/* Framebuffer file handle. */
	char *	env = 0;
	char *  fbp = 0;
	struct fb_var_screeninfo fb_var;
	struct fb_fix_screeninfo fb_fix;
	int x = 0, y = 0;
	char buff[65536]; //size is fb_fix.smem_len, the maximum number of bytes that can be read/wrote
	
	/* locate and open framebuffer, get info*/
	if(!(env = getenv("FRAMEBUFFER")))
		env = "/dev/fb0";
	fb = open(env, O_RDWR);

	if(fb < 0) {
		perror("Error: cannot open framebuffer device.\n");
		goto fail;
	}


	if( ioctl( fb, FBIOGET_FSCREENINFO, &fb_fix ) || ioctl( fb, FBIOGET_VSCREENINFO, &fb_var ) )
	{
	        perror("Error: getting screen info\n" );
		exit(1);
	}

	printf("%dx%d, %dbpp\n", fb_var.xres, fb_var.yres, fb_var.bits_per_pixel);

	// Map the device to memory
	fbp = fb_fix.smem_start;


	if ((int)fbp < 0) {
	        perror("Error mapping FB memory to user space\n" );
		goto fail;
	}

		
	/* clear the framebuffer */
	memset((void *) fb_fix.smem_start,0xff, fb_fix.smem_len);
	
	x =30; y =230;

	//draw R
	draw_square(fb, x, y, 8);
	draw_square(fb, x+1, y, 8);
	draw_square(fb, x+2, y, 8);
	draw_square(fb, x+2, y+8, 8);
	draw_square(fb, x, y+8, 8);
	draw_square(fb, x, y+16, 8);
	draw_square(fb, x, y+24, 8);
	draw_square(fb, x, y+32, 8);
	draw_square(fb, x+1, y+16, 8);
	draw_square(fb, x+2, y+16, 8);
	draw_square(fb, x+1, y+24, 8);
	draw_square(fb, x+2, y+32, 8);

	//draw T 
	x = x+4;
	draw_square(fb, x, y, 8);
	draw_square(fb, x+1, y, 8);
	draw_square(fb, x+2, y, 8);	
	draw_square(fb, x+3, y, 8);
	draw_square(fb, x+4, y, 8);
	draw_square(fb, x+2, y+8, 8);
	draw_square(fb, x+2, y+16, 8);
	draw_square(fb, x+2, y+24, 8);
	draw_square(fb, x+2, y+32, 8);
	
	//draw E
	x = x+6;
	draw_square(fb, x, y, 8);
	draw_square(fb, x+1, y, 8);
	draw_square(fb, x+2, y, 8);
	draw_square(fb, x, y+8, 8);
	draw_square(fb, x, y+16, 8);
	draw_square(fb, x, y+24, 8);
	draw_square(fb, x, y+32, 8);
	draw_square(fb, x+1, y+16, 8);
	draw_square(fb, x+2, y+16, 8);
	draw_square(fb, x+1, y+32, 8);
	draw_square(fb, x+2, y+32, 8);
 	
	//draw M
	x = x+4;
	draw_square(fb, x, y, 8);
	draw_square(fb, x, y+8, 8);
	draw_square(fb, x, y+16, 8);
	draw_square(fb, x, y+24, 8);
	draw_square(fb, x, y+32, 8);
	draw_square(fb, x+4, y, 8);
	draw_square(fb, x+4, y+8, 8);
	draw_square(fb, x+4, y+16, 8);
	draw_square(fb, x+4, y+24, 8);
	draw_square(fb, x+4, y+32, 8);
	draw_square(fb, x+1, y+8, 8);
	draw_square(fb, x+2, y+16, 8);
	draw_square(fb, x+3, y+8, 8);

	//raw S
	x = x+6;
	draw_square(fb, x, y, 8);
	draw_square(fb, x, y+8, 8);
	draw_square(fb, x, y+16, 8);
	draw_square(fb, x, y+32, 8);
	draw_square(fb, x, y+32, 8);
	draw_square(fb, x+1, y, 8);
	draw_square(fb, x+2, y, 8);
	draw_square(fb, x+1, y+16, 8);
	draw_square(fb, x+2, y+16, 8);
	draw_square(fb, x+2, y+24, 8);
	draw_square(fb, x+1, y+32, 8);
	draw_square(fb, x+2, y+32, 8);
		
	
	exit(0);
  	fail:
	close(fb);
#endif
}
